//
//  SendViewController.m
//  NiuNiuPacking
//
//  Created by shaoting on 14-9-7.
//  Copyright (c) 2014年 Reseek. All rights reserved.
//

#import "SendViewController.h"
#import "Toast+UIView.h"
#import "NSString+SBJSON.h"

@implementation SendViewController

- (void)dealloc {
    [_txtMobile release];
    [_txtSendNum release];
    [super dealloc];
}
- (IBAction)onClick:(id)sender {
    
    AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
    //请求url
    NSURL *url = [NSURL URLWithString:p_send_point_to_otherURL];
    
    //创建请求
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
    [request setHTTPMethod:@"POST"];//设置请求方式POST
    //token=d2b19008fe33b9480d423217d492c941&mobile=18918002424&count=100
    NSString *str = [NSString stringWithFormat:@"&token=%@&mobile=%@&count=%@",delegate.currentUser.token,self.txtMobile.text,self.txtSendNum.text];
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    
    //连接服务器
    NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    
    NSString *responseStr = [[NSString alloc]initWithData:received encoding:NSUTF8StringEncoding];
    NSDictionary *jsonDict = [responseStr JSONValue];
    
    int errnum = [jsonDict[@"errno"] intValue];
    if(jsonDict==nil)
    {
        [self.view makeToast:@"接口调用无返回值"
                    duration:1.0
                    position:@"bottom"];
        
    }else if(errnum == 0) {
        [self.view makeToast:@"送点成功！"
                    duration:1.0
                    position:@"bottom"];
        [self dismissViewControllerAnimated:YES completion:^{NSLog(@"送点成功！");}];
    }
    else {
        [self.view makeToast:jsonDict[@"errmsg"]
                    duration:1.0
                    position:@"bottom"];
        
    }
    
}
@end
